﻿namespace K9Nano.Updater.Server;

public interface IClientStore
{
    ValueTask<string?> GetSecretAsync(string clientId, CancellationToken cancellation = default);
    ValueTask<bool> EixtsAsync(string clientId, CancellationToken cancellation = default);
}


public class PlainClientStore : IClientStore
{
    private readonly IDictionary<string, string> _clients;

    public PlainClientStore(IEnumerable<KeyValuePair<string, string>> clients)
    {
        _clients = new Dictionary<string, string>(clients);
    }

    public PlainClientStore(Dictionary<string, string> clients)
    {
        _clients = clients;
    }

    public ValueTask<string?> GetSecretAsync(string clientId, CancellationToken cancellation = default)
    {
        _clients.TryGetValue(clientId, out var secret);

        return new ValueTask<string?>(secret);
    }

    public ValueTask<bool> EixtsAsync(string clientId, CancellationToken cancellation)
    {
        return new ValueTask<bool>(_clients.ContainsKey(clientId));
    }
}